<!--
 * @Description:维修建议 maintenanceAdvice
 * @Autor: 刘诗文
 * @Date: 2020-12-17 16:24:32
 * @LastEditors: 何兴家
 * @LastEditTime: 2021-02-23 09:02:39
-->
<template>
  <div>
    <FilterTable
      ref="labourTable"
      columnsRef="repari_repairReception_customer_repairSuggest_labour"
      height="auto"
      :columns="repairLabourColumns"
      :datakey="'rows'"
      :dataSource="labourDataSource"
      :onSyncTableData="handleSyncTableData"
      :isSelectColumn="isSelectColumn"
      :onRowSelectChange="
        list => {
          this.selectedLabourList = list;
        }
      "
      :onColumnsChange="columns => (this.repairLabourColumns = columns)"
    >
    </FilterTable>
    <FilterTable
      ref="partTable"
      columnsRef="repari_repairReception_customer_repairSuggest_part"
      height="auto"
      :columns="repairPartColumns"
      :datakey="'rows'"
      :dataSource="partDataSource"
      :onSyncTableData="handleSyncTableData"
      :isSelectColumn="isSelectColumn"
      :onRowSelectChange="
        list => {
          this.selectedPartList = list;
        }
      "
      :onColumnsChange="columns => (this.repairPartColumns = columns)"
    >
    </FilterTable>

    <div class="dialogBottmView">
      <el-button @click="handleCancel">取消</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { findTableItem } from '@/utils';
import { arrayObjDeWeight } from '@/utils/index';
import { getAllUser } from '@/api/customerRepair/usedCarVisit/resultEntry';

import { queryRepairSuggest } from '@/api/repair/repairReception/customer';

export default {
  props: {
    vin: String,
    isSelectColumn: {
      type: Boolean,
      default: true
    }
  },
  mixins: [dictionary],
  data() {
    this.selectedLabourList = []; // 选中的项目列表
    this.selectedPartList = []; // 选中的配件列表
    return {
      repairLabourColumns: this.createRepairLabourColumns(),
      repairPartColumns: this.createRepairPartColumns(),

      labourDataSource: [], // 项目数据源
      partDataSource: [], // 配件数据源
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    // 获取人员列表
    this.getAllUser();
    this.initQuery();
  },
  methods: {
    createRepairLabourColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '项目代码',
          dataIndex: 'LABOUR_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '项目名称',
          dataIndex: 'LABOUR_NAME',
          width: 200,
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '工时',
          dataIndex: 'STD_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '工时费',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '不修原因',
          dataIndex: 'REASON',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8070'),
          dictItems: this.createDictList('8070')
        },
        {
          title: '建议日期',
          dataIndex: 'SUGGEST_DATE',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '操作员',
          dataIndex: 'CREATED_BY',
          sorter: true,
          filter: true,
          filterType: 'input',
          dictItems: []
        },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发动机号',
          dataIndex: 'ENGINE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          filter: true,
          filterType: 'input',
          dictItems: []
        }
      ];
    },
    createRepairPartColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '配件数量',
          dataIndex: 'PART_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单价',
          dataIndex: 'PART_SALES_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2
        },
        {
          title: '维修项目代码',
          dataIndex: 'LABOUR_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '仓库',
          dataIndex: 'STORAGE_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作员',
          dataIndex: 'CREATED_BY',
          sorter: true,
          filter: true,
          filterType: 'input',
          dictItems: []
        },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发动机号',
          dataIndex: 'ENGINE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          filter: true,
          filterType: 'input',
          dictItems: []
        }
      ];
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    // 初始化查询
    async initQuery() {
      this.$refs.labourTable.START_LOADING();
      this.$refs.partTable.START_LOADING();
      try {
        let res = await queryRepairSuggest({ vin: this.vin });
        if (res.resultCode == 200) {
          // 处理部分数据
          res.data.partList.forEach(item => {
            item.PART_QUANTITY = item.QUANTITY;
          });
          this.labourDataSource = res.data.labourList;
          this.partDataSource = res.data.partList;
        }
      } catch (err) {
        console.log(err, '查询列表失败');
      } finally {
        this.$refs.labourTable.STOP_LOADING();
        this.$refs.partTable.STOP_LOADING();
      }
    },
    // 点击取消
    handleCancel() {
      this.$emit('close');
    },
    // 获取人员列表
    async getAllUser() {
      const res = await getAllUser();
      let data = arrayObjDeWeight(res.data, 'userId'); // 人员去重(解决前端报错)
      this.userList = data.map(x => ({
        value: x.userId + '',
        text: x.employeeName
      }));
      let targetTableItems = [
        findTableItem(this.repairLabourColumns, 'CREATED_BY'),
        findTableItem(this.repairLabourColumns, 'SERVICE_ADVISOR'),
        findTableItem(this.repairPartColumns, 'CREATED_BY'),
        findTableItem(this.repairPartColumns, 'SERVICE_ADVISOR')
      ];
      targetTableItems.forEach(item => {
        item.dictItems = this.userList;
      });
    }
  }
};
</script>

<style scoped></style>
